//
//  XQPositionDetailViewController.swift
//  XiaoQiQiaoSwift
//
//  Created by tech-lab on 2016/11/30.
//  Copyright © 2016年 校企桥. All rights reserved.
//
import Alamofire
import ObjectMapper
import AlamofireObjectMapper
import SwiftyJSON
import UIKit

class XQPositionDetailViewController: XQBaseViewController,UITableViewDataSource,UITableViewDelegate {
    
    var pid = String()
    var detailMode : XQPositionDetailMode?
    
    
    private  var tableView = UITableView()
    private let XQCourseSecondTableViewCellID = "XQCourseSecondTableViewCellID"
    private let  XQPositionDetailFirstTableViewCellID = "XQPositionDetailFirstTableViewCellID"
    private let XQPositionDetailSecondTableViewCellID = "XQPositionDetailSecondTableViewCell"
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationItem.title = "职位详情"
        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */
    
    
    
    
    override func setRootView() {
    
        self.tableView = UITableView.init(frame:  CGRect.zero, style: .grouped)
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        self.tableView.showsVerticalScrollIndicator = false
        self.view.addSubview(self.tableView)
        
        tableView.snp.makeConstraints { (make) in
            make.left.equalTo(self.view.snp.left)
            make.right.equalTo(self.view.snp.right)
            make.top.equalTo(self.view.snp.top)
            make.bottom.equalTo(self.view.snp.bottom)
        }
        
        self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
        self.tableView.register(XQPositionDetailFirstTableViewCell.self, forCellReuseIdentifier: XQPositionDetailFirstTableViewCellID)
        self.tableView.register(XQPositionDetailSecondTableViewCell.self, forCellReuseIdentifier: XQPositionDetailSecondTableViewCellID)
        self.tableView.register(XQCourseSecondTableViewCell.self, forCellReuseIdentifier: XQCourseSecondTableViewCellID)
        positionContentData()
        
    }
    
    private func positionContentData() {
        
        let urlstr = MTBaseURL.appending("NewVerRecruit/positionInfo.json")
        
        let para: Parameters = ["pid":pid]

        Alamofire.request(urlstr,parameters:para).responseObject { (response:DataResponse<XQPositionDetailMode>) in
            self.detailMode = response.result.value
            self.tableView.reloadData()
        }
        
    }
    

    
    
    
    func numberOfSections(in tableView: UITableView) -> Int {
        print("---------\nnumberOfSections")
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        print("numberOfRowsInSection")
        return 6
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        print("heightForHeaderInSection")
        return 10
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        print("heightForFooterInSection")
        return 0.1
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        print("heightForRowAt")

        if indexPath.row == 0 {
            return 60
        }else if indexPath.row == 1{
            return 80
        }else if indexPath.row == 2{
            return 40
        }else if indexPath.row == 3{
            if ((detailMode?.pd?.position_detail) != nil) {
                let size = detailMode?.pd?.position_detail?.boundingRect(with:CGSize.init(width: SCREEN_WIDTH-30, height: CGFloat.greatestFiniteMagnitude), options: [NSStringDrawingOptions.usesFontLeading,NSStringDrawingOptions.usesLineFragmentOrigin], attributes: nil, context: nil);
                
                if (size?.height)! > 40 {
                    return CGFloat((size?.height)!)+20
                }else{
                    return 40
                }
            }else{
                return 40
            }

        }else if indexPath.row == 4{
                return 40
        }else {
                return 100
        }
        
        
    }
    
    
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        print("cellForRowAt\n------------")

        var cell = tableView.dequeueReusableCell(withIdentifier: "cell")
        if cell == nil {
            cell = UITableViewCell.init(style: .value2, reuseIdentifier: "cell")
        }
        cell?.selectionStyle = .none

        if indexPath.row == 0 {
            let firstCell = tableView.dequeueReusableCell(withIdentifier: XQPositionDetailFirstTableViewCellID) as!XQPositionDetailFirstTableViewCell
            if ((detailMode?.pd) != nil) {
            firstCell.detailMode = detailMode?.pd
            }
            return firstCell
        }else if indexPath.row == 1{
            let secondCell = tableView.dequeueReusableCell(withIdentifier: XQPositionDetailSecondTableViewCellID) as!XQPositionDetailSecondTableViewCell
            if ((detailMode?.pd) != nil) {
            secondCell.detailMode = detailMode?.pd
            }
            return secondCell
            
        }else if indexPath.row == 2{
            cell?.imageView?.image = UIImage.init(named: "zhimiaoshu")
            cell?.textLabel?.text = "职位描述"
            return cell!
        }else if indexPath.row == 3{
            cell?.textLabel?.numberOfLines = 0
            cell?.textLabel?.font = UIFont .systemFont(ofSize: 12)
            if ((detailMode?.pd) != nil) {
                cell?.textLabel?.text = detailMode?.pd?.position_detail
            }
            return cell!
        }else if indexPath.row == 4{
            //cell?.imageView?.image = UIImage.init(named: "")
            cell?.textLabel?.text = "相关课程"
            return cell!
        }else{
          let relateCourseCell =  tableView.dequeueReusableCell(withIdentifier: XQCourseSecondTableViewCellID) as!XQCourseSecondTableViewCell
            if ((detailMode?.cd) != nil) {
                relateCourseCell.dataArray = (detailMode?.cd)!
                relateCourseCell.collectionView.reloadData()
            }
            return relateCourseCell
        }
        
        
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        if indexPath.row == 1 {
            let companydetailVC = XQPositionCompanyDetailViewController()
            companydetailVC.cid = (self.detailMode?.pd?.member_id)!
            self.navigationController?.pushViewController(companydetailVC, animated: true)
        }
    
    }
    

}
